Imports System.IO

Friend MustInherit Class License
    Public Enum Level As Integer
        Evaluation = 0
        Registered = 1
    End Enum

    Private msKey As String = ""
    Private mlClientID As Long = -1
    Private miFunctionalityLevel As Level = Level.Evaluation

    Public Sub New(ByVal sKey As String)
        msKey = sKey
    End Sub

    Public Property ClientID() As Integer
        Get
            Return mlClientID
        End Get
        Set(ByVal value As Integer)
            mlClientID = value
        End Set
    End Property

    Public Property FunctionalityLevel() As Level
        Get
            Return miFunctionalityLevel
        End Get
        Set(ByVal value As Level)
            miFunctionalityLevel = value
        End Set
    End Property

    Public Sub ReadLicenseFile(ByVal sFileName As String)
        miFunctionalityLevel = Level.Evaluation

        Try
            If File.Exists(sFileName) Then
                '---Read file
                Dim oReader As New StreamReader(sFileName)
                Dim sText As String = oReader.ReadToEnd
                oReader.Close()
                oReader.Dispose()

                '---Attempt to decrypt file
                Dim oCrypt As New JCO.Crypt(msKey, False)
                Dim sUnencryptedText As String = oCrypt.Decrypt(sText)

                '---Get Client ID
                Dim sClientID As String = GetTagValue(sUnencryptedText, "i")

                '---Get hash
                Dim sHash As String = GetTagValue(sUnencryptedText, "h")

                If sClientID > "" AndAlso sHash > "" Then
                    '---Remember client id
                    mlClientID = Val(sClientID)

                    '---Verify hash
                    Dim oJCO As New JCO
                    If oJCO.GetHash("Registered") = sHash Then
                        miFunctionalityLevel = Level.Registered
                    End If
                End If
            End If

        Catch oEX As Exception
            '---If there is any kind of error, assume that the license file is invalid
        End Try
    End Sub

    Protected Sub WriteLicenseFile(ByVal sFileName As String)
        Try
            Dim oJCO As New JCO
            Dim sHash As String = ""

            If miFunctionalityLevel = Level.Registered Then
                sHash = oJCO.GetHash("Registered")
            Else
                sHash = oJCO.GetHash("Unregistered")
            End If

            '---Compose license
            Dim sText As String = "<i>" & mlClientID & "</i>" & _
                                  "<h>" & sHash & "</h>"

            '---Encrypt license
            Dim oCrypt As New JCO.Crypt(msKey, False)
            Dim sEncryptedText As String = oCrypt.Encrypt(sText)

            '---Write file
            Dim oWriter As New StreamWriter(sFileName, False)
            oWriter.Write(sEncryptedText)
            oWriter.Flush()
            oWriter.Close()
            oWriter.Dispose()

        Catch oEX As Exception
            '---If there is any kind of error, assume that the license file is invalid
        End Try
    End Sub

    Private Function GetTagValue( _
                        ByVal sLicenseText As String, _
                        ByVal sTag As String) As String

        Dim sResult As String = ""

        Dim iSLoc As Integer = sLicenseText.IndexOf("<" & sTag & ">")

        If iSLoc > -1 Then
            Dim iELoc As Integer = sLicenseText.IndexOf("</" & sTag & ">", iSLoc)

            If iELoc > -1 Then
                sResult = sLicenseText.Substring(iSLoc + 3, iELoc - (iSLoc + 3))
            End If
        End If

        Return sResult
    End Function
End Class
